<?php

namespace App\Services;

use App\Models\Crm_Care;
use App\Models\Crm_Visit;
use App\Traits\External;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;

class InteractService
{
    public function getVisitList($params)
    {
        $result = Crm_Visit::getFilter($params)
            ->Nodel()
            ->paginate($params['pageSize']);
        $list['total'] = $result->total();

        $result = External::optionResult($result,3);
        $list['list'] = $result;
        return $list;
    }

    public function visitDel($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();

            $res = Crm_Visit::find($params['id']);
            $res->is_del = 'Y';
            if(!$res->save()){
                throw new \Exception("删除Crm_Visit浏览记录失败".$params['id']);
            }

            DB::commit();
            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function getCareList($params)
    {
        $result = Crm_Care::getFilter($params)
            ->Nodel()
            ->paginate($params['pageSize']);
        $list['total'] = $result->total();

        $result = External::optionResult($result,3);
        $list['list'] = $result;
        return $list;
    }

    public function careDel($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();

            $res = Crm_Care::find($params['id']);
            $res->is_del = 'Y';
            if(!$res->save()){
                throw new \Exception("删除Crm_Care关注记录失败".$params['id']);
            }

            DB::commit();
            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }
}
